package me.hetong.caigou.ui.view;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.*;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import me.hetong.caigou.Global;
import me.hetong.caigou.ui.chessboard.ChessBoardWar;
import me.hetong.caigou.ui.chessboard.ChessboardWarImage;
import me.hetong.caigou.entity.Zhao;
import me.hetong.caigou.util.FenUtils;
import me.hetong.caigou.util.FxUtils;

import java.awt.*;
import java.io.File;

/**
 * 顶部菜单栏
 */
public class InferTopMenuBar {

    public static MenuBar init() {

        Menu menu1 = new Menu("棋谱");
        Menu menu2 = new Menu("界面");
        Menu menu3 = new Menu("引擎");
        Menu menu4 = new Menu("设置");
        Menu menu5 = new Menu("帮助");

        MenuBar menuBar = new MenuBar();
//        menuBar.setPrefWidth(config.getWidth() - config.getLeftSideWidth());
        menuBar.getMenus().add(menu1);
        menuBar.getMenus().add(menu2);
        menuBar.getMenus().add(menu3);
        menuBar.getMenus().add(menu4);
        menuBar.getMenus().add(menu5);

        // 棋谱
        {
            MenuItem menuItem1 = new MenuItem("棋谱管理m");
            menu1.getItems().add(menuItem1);
//            menuItem1.setOnAction(actionEvent -> PuManageStage.stage.show());

            MenuItem menuItem2 = new MenuItem("粘贴东萍棋谱");
            menu1.getItems().add(menuItem2);
            menuItem2.setOnAction(actionEvent -> {
            });

            MenuItem menuItem3 = new MenuItem("保存棋谱s");
            menu1.getItems().add(menuItem3);
            menuItem3.setOnAction(actionEvent -> Global.saveWen());
        }

        // 界面
        {
            MenuItem menuItem4 = new MenuItem("复制当前局面FEN");
            menu2.getItems().add(menuItem4);
            menuItem4.setOnAction(event -> {
                final ClipboardContent content = new ClipboardContent();
                content.putString(FenUtils.chessListToFen(ChessboardWarImage.chessList));
                final Clipboard clipboard = Clipboard.getSystemClipboard();
                clipboard.setContent(content);
            });

            MenuItem menuItem5 = new MenuItem("粘贴FEN到当前局面");
            menu2.getItems().add(menuItem5);
            menuItem5.setOnAction(event -> {
                final Clipboard clipboard = Clipboard.getSystemClipboard();
                String content = (String) clipboard.getContent(DataFormat.PLAIN_TEXT);
                if (StrUtil.isNotBlank(content)) {
                    ChessboardWarImage.initChessList(content);
                }
            });

            menu2.getItems().add(new SeparatorMenuItem());

            MenuItem menuItem11 = new MenuItem("开局u");
            menu2.getItems().add(menuItem11);
            menuItem11.setOnAction(event -> ChessBoardWar.begin());

            MenuItem menuItem12 = new MenuItem("上一着l");
            menu2.getItems().add(menuItem12);
            menuItem12.setOnAction(event -> ChessBoardWar.prev());

            MenuItem menuItem13 = new MenuItem("下一着r");
            menu2.getItems().add(menuItem13);
            menuItem13.setOnAction(event -> ChessBoardWar.next());

            MenuItem menuItem14 = new MenuItem("终局e");
            menu2.getItems().add(menuItem14);
            menuItem14.setOnAction(event -> ChessBoardWar.end());
        }

        // 设置
        {
            MenuItem menuItem6 = new MenuItem("专家设置");
            menu4.getItems().add(menuItem6);
            menuItem6.setOnAction(event -> {
                try {
                    Desktop.getDesktop().open(new File("config.json"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            MenuItem menuItem7 = new MenuItem("重载配置");
            menu4.getItems().add(menuItem7);
        }

        // 引擎
        {
            CheckMenuItem menuItem8 = FxUtils.createCheckMenuItem("引擎开关e");
            menu3.getItems().add(menuItem8);
            menuItem8.setOnAction(actionEvent -> {
                if (menuItem8.isSelected()) {
                    Global.engine.startUp();
                } else {
                    Global.engine.shutdown();
                }
            });

            MenuItem menuItem10 = new MenuItem("分析棋局");
            menu3.getItems().add(menuItem10);
            menuItem10.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent actionEvent) {
                }
            });

            CheckMenuItem menuItem1 = FxUtils.createCheckMenuItem("着法重搜p");
            menu3.getItems().add(menuItem1);
            menuItem1.setOnAction(actionEvent -> Global.engine.reSearch());
        }

        return menuBar;
    }
}